package cn.itsy.web.controller;

import cn.itsy.dao.Pactypedao;
import cn.itsy.dao.Pagedao;
import cn.itsy.pojo.Pactype;
import cn.itsy.pojo.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("pactype")
public class PactypeController {

    @Resource
    private Pactypedao pd;
    @Resource
    private Pagedao pad;

    @RequestMapping("list")
    public String list(Pactype p, Model model, String currentPage) {
        Page page = new Page();
        String sql = "select * from pactype where 1=1";
        if (p.getCtno() != null && !p.getCtno().equals("")) {
            sql += " and ctno = '" + p.getCtno() + "'";
        }
        if (p.getCtname() != null && !p.getCtname().equals("")) {
            sql += " and ctname like '%" + p.getCtname() + "%'";
        }
        if (p.getCtdesc() != null && !p.getCtdesc().equals("")) {
            sql += " and ctdesc like '%" + p.getCtdesc() + "%'";
        }
        Page pactype = pad.pactypefy(sql, currentPage, page);
        model.addAttribute("pactype", pactype);
        return "/pactype";
    }

    @RequestMapping("add")
    public String add(Pactype p) {
        String sql = "insert into pactype values (null,'" + p.getCtno() + "','" + p.getCtname() + "','" + p.getCtdesc() + "','" + p.getStatus() + "')";
        pd.add(sql);
        return "redirect:list";
    }

    @RequestMapping("toupdate")
    public String toupdate(String id, Model model) {
        String sql = "select * from pactype where 1=1 and id='" + id + "'";
        List<Pactype> list = pd.list(sql);
        model.addAttribute("list", list);
        return "/pactypeupdate";
    }

    @RequestMapping("update")
    public String update(Pactype p) {
        String sql = "update pactype set ctno='" + p.getCtno() + "',ctname='" + p.getCtname() + "',ctdesc='" + p.getCtdesc() + "',status='" + p.getStatus() + "' where id='" + p.getId() + "'";
        pd.update(sql);
        return "redirect:list";
    }

    @RequestMapping("delete")
    public String delete(String[] ids, String id) {
        //System.out.println(ids);
        System.out.println(id);
        String sql = "";
        if (ids != null) {
            for (int i = 0; i < ids.length; i++) {
                sql = "delete from pactype where id='" + ids[i] + "'";
                pd.delete(sql);
            }
        }
        sql = "delete from pactype where id='" + id + "'";
        pd.delete(sql);
        return "redirect:list";
    }
}
